package cn.iocoder.yudao.module.student.controller.app.collectCompany;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.company.CompanyApi;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyRespDTO;
import cn.iocoder.yudao.module.member.api.job.JobApi;
import cn.iocoder.yudao.module.member.api.job.dto.JobRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.student.controller.app.collectCompany.vo.*;
import cn.iocoder.yudao.module.student.dal.dataobject.collectCompany.CollectCompanyDO;
import cn.iocoder.yudao.module.student.convert.collectCompany.CollectCompanyConvert;
import cn.iocoder.yudao.module.student.service.collectCompany.CollectCompanyService;

@Api(tags = "用户 APP - 收藏企业")
@RestController
@RequestMapping("/student/collect-company")
@Validated
public class AppCollectCompanyController {

    @Resource
    private CollectCompanyService collectCompanyService;
    @Resource
    private CompanyApi companyApi;

    @Resource
    private JobApi jobApi;
    @PostMapping("/create")
    @ApiOperation("创建收藏企业")

    public CommonResult<Long> createCollectCompany(@Valid @RequestBody AppCollectCompanyCreateReqVO createReqVO) {
        return success(collectCompanyService.createCollectCompany(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新收藏企业")

    public CommonResult<Boolean> updateCollectCompany(@Valid @RequestBody AppCollectCompanyUpdateReqVO updateReqVO) {
        collectCompanyService.updateCollectCompany(updateReqVO);
        return success(true);
    }
    @PostMapping("/createOrUpdateCollectCompany")
    @ApiOperation("收藏/取消收藏企业")
    public CommonResult<Boolean> createOrUpdateCollectCompany(@Valid @RequestBody AppCollectCompanyUpdateReqVO createReqVO) {
        collectCompanyService.createOrUpdateCollectCompany(createReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除收藏企业")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)

    public CommonResult<Boolean> deleteCollectCompany(@RequestParam("id") Long id) {
        collectCompanyService.deleteCollectCompany(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得收藏企业")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)

    public CommonResult<AppCollectCompanyRespVO> getCollectCompany(@RequestParam("id") Long id) {
        CollectCompanyDO collectCompany = collectCompanyService.getCollectCompany(id);
        CompanyRespDTO companyRespDTO=companyApi.getCompanyById(collectCompany.getCompanyId());
        List<JobRespDTO>  memberCompanyJobDOList=jobApi.getJobListTop2(companyRespDTO.getId());
        String  jobJson= JsonUtils.toJsonString(memberCompanyJobDOList);
        //有则json对象 设置进去
        companyRespDTO.setCompanyJobDOList(jobJson);
        AppCollectCompanyRespVO appCollectCompanyRespVO=CollectCompanyConvert.INSTANCE.convert(collectCompany);
        appCollectCompanyRespVO.setCompanyRespDTO(companyRespDTO);
        return success(appCollectCompanyRespVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得收藏企业列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)

    public CommonResult<List<AppCollectCompanyRespVO>> getCollectCompanyList(@RequestParam("ids") Collection<Long> ids) {
        List<CollectCompanyDO> list = collectCompanyService.getCollectCompanyList(ids);
        return success(CollectCompanyConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得收藏企业分页")
    public CommonResult<PageResult<CompanyRespDTO>> getCollectCompanyPage(@RequestBody AppCollectCompanyPageReqVO pageVO) {
        pageVO.setStudentId(SecurityFrameworkUtils.getLoginUserId());
        PageResult<CollectCompanyDO> pageResult = collectCompanyService.getCollectCompanyPage(pageVO);
        List<CollectCompanyDO> collectCompanyDOList=pageResult.getList();
        List<CompanyRespDTO> companyRespDTOs=new ArrayList<>();
        Map<Long,Date> timeMap=new HashMap<>();
        for (CollectCompanyDO collectCompanyDO: collectCompanyDOList) {
            CompanyRespDTO companyRespDTO= companyApi.getCompanyById(collectCompanyDO.getCompanyId());
            List<JobRespDTO> jobRespDTOS =jobApi.getJobListTop2(companyRespDTO.getId());
            if(!jobRespDTOS.isEmpty()){
                String jobJson=JsonUtils.toJsonString(jobRespDTOS);
                companyRespDTO.setCompanyJobDOList(jobJson);
            }
            companyRespDTO.setCollectTime(timeMap.get(collectCompanyDO.getCollectTime()));
            companyRespDTOs.add(companyRespDTO);
        }
        if(collectCompanyDOList.isEmpty()){
            return success(null);
        }

        PageResult<CompanyRespDTO> companyRespDTOPageResult=new PageResult<>();
        companyRespDTOPageResult.setTotal(pageResult.getTotal());
        companyRespDTOPageResult.setList(companyRespDTOs);
        return success(companyRespDTOPageResult);
    }

    @PostMapping("/isCollect")
    @ApiOperation("获得收藏企业分页")
    public CommonResult<Boolean> isCollect(@RequestBody AppCollectCompanyPageReqVO pageVO) {
       return success(collectCompanyService.isCollect(pageVO));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出收藏企业 Excel")

    @OperateLog(type = EXPORT)
    public void exportCollectCompanyExcel(@Valid AppCollectCompanyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CollectCompanyDO> list = collectCompanyService.getCollectCompanyList(exportReqVO);
        // 导出 Excel
        List<AppCollectCompanyExcelVO> datas = CollectCompanyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "收藏企业.xls", "数据", AppCollectCompanyExcelVO.class, datas);
    }

}
